package com.weishu.system.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.weishu.common.core.text.UUID;
import com.weishu.common.core.utils.poi.ExcelUtil;
import com.weishu.common.core.web.controller.BaseController;
import com.weishu.common.core.web.domain.AjaxResult;
import com.weishu.common.core.web.page.TableDataInfo;
import com.weishu.common.log.annotation.Log;
import com.weishu.common.log.enums.BusinessType;
import com.weishu.common.security.annotation.PreAuthorize;
import com.weishu.system.config.PicUploadResult;
import com.weishu.system.domain.TbBusiness;
import com.weishu.system.service.ITbBusinessService;
import com.weishu.system.utils.UploadPicService;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商户Controller
 *
 * @author weishu
 * @date 2021-11-15
 */
@RestController
@RequestMapping("/business")
public class TbBusinessController extends BaseController
{
    @Autowired
    private ITbBusinessService tbBusinessService;
    @Autowired
    private UploadPicService uploadPicService;

    /**
     * 查询商户列表
     */
    @PreAuthorize(hasPermi = "system:business:list")
    @GetMapping("/list")
    public TableDataInfo list(TbBusiness tbBusiness)
    {
        startPage();
        java.util.List<TbBusiness> list = tbBusinessService.selectTbBusinessList(tbBusiness);
        return getDataTable(list);
    }

    /**
     * 导出商户列表
     */
    @PreAuthorize(hasPermi = "system:business:export")
    @Log(title = "商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbBusiness tbBusiness) throws IOException
    {
        java.util.List<TbBusiness> list = tbBusinessService.selectTbBusinessList(tbBusiness);
        ExcelUtil<TbBusiness> util = new ExcelUtil<TbBusiness>(TbBusiness.class);
        util.exportExcel(response, list, "business");
    }

    /**
     * 获取商户详细信息
     */
    @PreAuthorize(hasPermi = "system:business:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbBusinessService.selectTbBusinessById(id));
    }

    /**
     * 新增商户
     */
    @PreAuthorize(hasPermi = "system:business:add")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TbBusiness tbBusiness)
    {
        return toAjax(tbBusinessService.insertTbBusiness(tbBusiness));
    }

    /**
     * 修改商户
     */
    @PreAuthorize(hasPermi = "system:business:updateBusiness")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PostMapping("/updateBusiness")
    public AjaxResult updateBusiness(@RequestBody TbBusiness tbBusiness)
    {
        System.err.println(tbBusiness);
        return tbBusinessService.updateTbBusiness(tbBusiness);
    }

    /**
     * 删除商户
     */
    @PreAuthorize(hasPermi = "system:business:remove")
    @Log(title = "商户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbBusinessService.deleteTbBusinessByIds(ids));
    }


    /*
    * 图片上传oss
    * */
    //@PreAuthorize(hasPermi = "system:business:oss")
    @Log(title = "商户",businessType = BusinessType.IMPORT)
    @PostMapping(value = "/uploadImg")
    public PicUploadResult  uploadImg(MultipartFile file) {
        PicUploadResult upload = uploadPicService.upload(file);
        System.err.println(upload);
        return upload;
    }
}
